package util;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

public class DateUtil {

    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private final static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final static SimpleDateFormat sdfHHMMSS = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    private final static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
    private final static SimpleDateFormat yyyyNMMYddR = new SimpleDateFormat("yyyy年MM月dd日");
    private final static SimpleDateFormat week = new SimpleDateFormat("EE");
    private final static SimpleDateFormat yyyymmddee = new SimpleDateFormat("yyyy-MM-dd EE");

    private final static SimpleDateFormat yyyyMMddhhmmssSSS = new SimpleDateFormat("yyyyMMddhhmmssSSS");
    private final static SimpleDateFormat sdf15 = new SimpleDateFormat("HHmm");

    public static String simpleDate3(Date date1) {
        if (date1 == null)
            return "";
        return sdf2.format(date1);
    }

    public static String simpleDate1(String date1) {
        if (date1 == null)
            return "";
        return sdf.format(date1);
    }
    public static String yyyyNMMYddR(Date date1) {
        if (date1 == null)
            return "";
        return yyyyNMMYddR.format(date1);
    }
    public static String simpleDate2(Date date) {
        if (yyyyMMddhhmmssSSS == null)
            return null;
        return yyyyMMddhhmmssSSS.format(date);

    }
    /**
     * 取当前时间
     *
     * @return
     */
    public static String getCurrentTime() {

        Calendar calendar = Calendar.getInstance();

        int i = calendar.get(1);
        int j = calendar.get(2) + 1;
        int k = calendar.get(5);
        return "" + i + (j >= 10 ? "" + j : "0" + j) + (k >= 10 ? "" + k : "0" + k);
    }
    public static Date calculateDate(Date startDay, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDay);
        cal.add(Calendar.DAY_OF_YEAR, days);
        return cal.getTime();
    }
    public static Date calculateMonth(Date startDay, int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDay);
        cal.add(Calendar.MONTH, months);
        return cal.getTime();
    }

    public static Date calculateMin(Date startDay, int min) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDay);
        cal.add(Calendar.MINUTE, min);
        return cal.getTime();
    }
    /**
     * String 获取今天日期yyyyMMdd
     *
     * @return
     */
    public static String getToday() {
        Calendar calendar = Calendar.getInstance();
        return DateUtil.getyyyyMMdd(calendar.getTime());
    }
    /**
     * String 获取今天日期yyyy-MM-dd
     *
     * @return
     */
    public static String getToday2() {
        Calendar calendar = Calendar.getInstance();
        return DateUtil.getyyyy_MM_dd(calendar.getTime());
    }
    /**
     * String 获取几天后日期yyyyMMdd
     *
     * @return
     */
    public static String getDateByDays(Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return DateUtil.getyyyyMMdd(calendar.getTime());
    }
    /**
     * String 获取几天后日期yyyy-MM-dd
     *
     * @return
     */
    public static String getDateByDays2(Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return DateUtil.getyyyy_MM_dd(calendar.getTime());
    }
    /**
     * 将字符串化为日期格式
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    public static Date strToDate(String time, String pattern) {
        if (StringUtils.isBlank(time)) {
            return new Date();
        }
        try {
            return new SimpleDateFormat(pattern).parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static String getWeek(Date date) {
        if (date == null)
            return "";
        return week.format(date);
    }
    public static String getyyyyMMdd(Date date) {
        if (date == null)
            return "";
        return yyyyMMdd.format(date);
    }
    public static String getyyyy_MM_dd(Date date) {
        if (date == null)
            return "";
        return sdf.format(date);
    }
    public static String getDateToString2(Date date) {
        if (date == null)
            return "";
        return sdf2.format(date);
    }
    public static String getyyyymmddee(Date date) {
        if (date == null)
            return "";
        return yyyymmddee.format(date);
    }
    public static Date getyyyy_MM_dd(String date) {
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
        }
        return new Date();
    }
    public static Date StringToDate(String date) {
        try {
            return yyyyMMdd.parse(date);
        } catch (ParseException e) {
        }
        return new Date();
    }
    /**
     * String 获取时间 yyyy-MM-dd HH:mm
     *
     * @param date
     * @return
     */
    public static String format1(Date date) {
        if (date == null)
            date = new Date();
        return sdf1.format(date);
    }
    public static String dateToString1(Date date) {
        try {
            return sdf1.format(date);
        } catch (Exception e) {
        }
        return null;
    }
    /**
     * Date 获取时间 yyyy-MM-dd HH:mm
     *
     * @return Date
     */
    public static Date StringToDate1(String date) {
        try {
            return sdf1.parse(date);
        } catch (ParseException e) {
        }
        return new Date();
    }
    /**
     * Date 获取时间 yyyy-MM-dd HH:mm
     *
     * @return Date
     */
    public static Date StringToDate2(String date) {
        try {
            return sdf2.parse(date);
        } catch (ParseException e) {
        }
        return new Date();
    }
    /**
     * Date 获取时间yyyy-MM-dd HH:mm:ss
     *
     * @return Date
     */
    public static Date StringToDate3(String date) {
        try {
            return sdf2.parse(date);
        } catch (ParseException e) {
        }
        return new Date();
    }
    /**
     * Date 获取时间 yyyy/MM/dd
     *
     * @return Date
     */
    public static Date StringToDate5(String date) {
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
        }
        return new Date();
    }
    /**
     * yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static Date StringToDate7(String date) {
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
        }
        return new Date();
    }

    /**
     * Date 获取时间 yyyy/MM/dd HH:mm:ss
     *
     * @return Date
     */
    public static Date sdfHHMMSS(String date) {
        try {
            return sdfHHMMSS.parse(date);
        } catch (ParseException e) {
        }
        return new Date();
    }
    /**
     * 获取周几
     *
     * @param date yyyyMMdd
     * @return
     */
    public static String getWeek(String date) {
        String week = "";
        switch (getWeekId(date)) {
            case 1:
                week = "周一";
                break;
            case 2:
                week = "周二";
                break;
            case 3:
                week = "周三";
                break;
            case 4:
                week = "周四";
                break;
            case 5:
                week = "周五";
                break;
            case 6:
                week = "周六";
                break;
            case 0:
                week = "周日";
                break;

            default:
                break;
        }
        return week;
    }
    public static Integer getWeekId(String date) {
        Date d = new Date();
        try {
            d = yyyyMMdd.parse(date);
        } catch (ParseException e) {
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        Integer i = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return i;
    }
    /**
     * 时间计算（日）
     *
     * @param date
     * @param n
     * @return
     */
    public static Date getDate(Date date, int n) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(Calendar.DAY_OF_MONTH, n);
        return gc.getTime();
    }
    public static Date getDate(Date date, int field, int n) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(field, n);
        return gc.getTime();
    }
    public static String stringToDate15(Date date) {
        if (date == null)
            return "";
        return sdf15.format(date);

    }
    public static String getyyyy_MM_dd2(String date) throws ParseException {
        if (date == null)
            return "";
        return sdf.format(sdf.parse(date));
    }
    public static Date getyyyy_MM_dd3(String date) throws ParseException {
        if (date == null)
            return null;
        return sdf.parse(date);
    }
    // 制定日期的后一天
    public static String getSpecifiedDayAfter(String specifiedDay) throws ParseException {
        Calendar c = Calendar.getInstance();
        Date date = null;
        date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

        return dayAfter;

    }
    // 制定日期的前一天
    public static String getSpecifiedYesterday(String specifiedDay) throws ParseException {
        Calendar c = Calendar.getInstance();
        Date date = null;
        date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

        return dayAfter;

    }
    public static int getDaysBetween(Map<String, Object> params) {

        String start = params.get("start_date").toString();
        String end = params.get("end_date").toString();

        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();

        Date startDate = null;

        Date endDate = null;

        try {
            startDate = new SimpleDateFormat("yy-MM-dd").parse(start);
            startCalendar.setTime(startDate);
            endDate = new SimpleDateFormat("yy-MM-dd").parse(end);
            endCalendar.setTime(endDate);
        } catch (ParseException e) {
        }
        int days = endCalendar.get(Calendar.DAY_OF_YEAR) - startCalendar.get(Calendar.DAY_OF_YEAR);

        int y2 = endCalendar.get(Calendar.YEAR);

        if (startCalendar.get(Calendar.YEAR) != y2) {
            startCalendar = (Calendar) startCalendar.clone();
            do {
                days += startCalendar.getActualMaximum(Calendar.DAY_OF_YEAR);
                startCalendar.add(Calendar.YEAR, 1);
            } while (startCalendar.get(Calendar.YEAR) != y2);
        }
        return days;
    }
    public String simpleDate(Date date) {
        if (date == null)
            return "";
        return sdf.format(date);
    }
    /**
     * mongodb默认时区为UTC
     * 北京时间要+8H
     * @param date
     * @return
     */
    public static Date getMongoDate(Date date) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.HOUR_OF_DAY, 8);
        return StringToDate3(sdf2.format(ca.getTime()));
    }

    public static String getWarningTime(Date date) {
        return dateToString(date, "yyyy-MM-dd'T'HH:mm:ss");
    }

    public static Date getWarningTime(String time) {
        return strToDate(time, "yyyy-MM-dd'T'HH:mm:ss");
    }
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.getFirstDayOfWeek();
//        c.get
    }

    public static boolean isTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d = sdf.parse(time);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }
}
